﻿var scrollTop = 0;

Array.prototype.insert = function (index, item) {
    this.splice(index, 0, item);
};

$(document).ready(function () {
    paginate("pagination", $("#totalPage").val(), $("#pageIndex").val());
});

function paginate(className, totalPage, pageIndex) {
    if ($("." + className).length == 0) {
        return false;
    }

    totalPage = totalPage == 0 ? 1 : totalPage;

    var index = 1;
    var first = $("<a class='paginate" + (pageIndex == 1 ? " disabled" : "") + "' href='#'><<</a>");
    var previous = $("<a class='paginate" + (pageIndex == 1 ? " disabled" : "") + "' href='#'><</a>");
    var next = $("<a class='paginate" + (pageIndex == totalPage ? " disabled" : "") + "' href='#'>></a>");
    var last = $("<a class='paginate" + (pageIndex == totalPage ? " disabled" : "") + "' href='#'>>></a>");

    $("." + className).empty();
    $("." + className).append(first);
    $("." + className).append(previous);

    if (totalPage - pageIndex < 8 && totalPage - 8 > 0) {
        index = totalPage - 8;
    } 

    for (var i = 0; index <= totalPage && i < 8; index++, i++) {
        var pageLink = $('<a class="paginate" href="#">' + index + '</a>');
        if (index == pageIndex) {
            pageLink.addClass("selected");
        }
        $("." + className).append(pageLink);
    }

    $("." + className).append(next);
    $("." + className).append(last);

    bindingPaginationEvent(className, totalPage);

    return true;
}

function bindingPaginationEvent(className, totalPage) {
    $("." + className + " a.paginate").click(function () {
        if ($(this).hasClass("disabled") || $(this).hasClass("selected")) {
            return false;
        }

        var pageIndex;
        var value = $(this).text().trim();
        var currentPage = parseInt($("." + className + " a.selected").text().trim());
        switch (value) {
            case "<<":
                pageIndex = 1;
                break;
            case "<":
                pageIndex = (currentPage == 1 ? 1 : currentPage - 1);
                break;
            case ">":
                pageIndex = (currentPage == totalPage ? totalPage : currentPage + 1);
                break;
            case ">>":
                pageIndex = totalPage;
                break;
            default:
                pageIndex = value;
                break;
        }

        $("#pageIndex").val(pageIndex);

        return false;
    });
}

// Check the page is in iframe.
function checkInIframe(w) {
    if (w == null) {
        w = window;
    }

    return (w.location != w.parent.location);
}

// Redirect to top of parent page to login page
function redirectToLogin(w) {
    if (w == null) {
        w = window;
    }

    if (checkInIframe(w)) {
        redirectToLogin(w.parent);
    }
    else {
        w.top.location = "/Master/Index";
        alert("Bạn đã ngừng xử lý quá lâu.\nHãy đăng nhập lại để xác nhận bạn là chủ tài khoản này!.");
    }

    return false;
}

function redirectToPage(controller, action, parram) {
    var host = window.location.origin;
    window.location = host + "/" + controller + "/" + action + "?" + parram;
    return false;
}

function getScreenHeight(w) {
    var screenHeight = 0;
    if (w == null) {
        w = window;
    }

    if (checkInIframe(w)) {
        screenHeight = getScreenHeight(w.parent);
    }
    else {
        screenHeight = w.innerHeight;
    }

    return screenHeight;
}

function getScreenWidth(w) {
    var screenWidth = 0;
    if (w == null) {
        w = window;
    }

    if (checkInIframe(w)) {
        screenWidth = getScreenHeight(w.parent);
    }
    else {
        screenWidth = w.innerWidth;
    }

    return screenWidth;
}

function getTopWindow(w) {
    if (w == null) {
        w = window;
    }

    if (checkInIframe(w)) {
        return getTopWindow(w.parent);
    }
    else {
        return w;
    }
}

function lockScroll() {
    var w = getTopWindow();
    scrollTop = $(w).scrollTop();

    $(w.document).find("body, html").css({
        "overflow": "hidden"
    });
}

function unlockScroll() {
    var w = getTopWindow();

    $(w.document).find("body, html").css({
        "overflow": ""
    });

    $(w).scrollTop(scrollTop);
}